package raf;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 *
 */
public class RAFDemo3 {
    public static void main(String[] args) throws IOException {
        RandomAccessFile raf=new RandomAccessFile("fra.dat","rw");
        System.out.println("pointer:"+raf.getFilePointer());
        /**
         * long getFilePointer()
         * 获取当前RAF的指针位置（下标）
         */
        //将一个int值写入文件中
        int i =Integer.MAX_VALUE;
        /*
           i的2进制：                   vvvvvvvv
           01111111 11111111 11111111 11111111
               1         2        3       4
               i>>>24
               00000000 00000000 00000000 00000001
               00000000 00000000 01111111 11111111
               00000000 01111111 11111111 11111111
               01111111 11111111 11111111 11111111
         */
        raf.write(i>>>24);
        System.out.println("pointer:"+raf.getFilePointer());
        raf.write(i>>>16);
        System.out.println("pointer:"+raf.getFilePointer());
        raf.write(i>>>8);
        System.out.println("pointer:"+raf.getFilePointer());
        raf.write(i);
        System.out.println("pointer:"+raf.getFilePointer());
        /**
         * RAF提供了方便写出基本类型的相关方法
         */
       raf.writeInt(i);
        System.out.println("pointer:"+raf.getFilePointer());
        raf.writeDouble(123.123);
       System.out.println("pointer:"+raf.getFilePointer());
        raf.writeLong(123L);
        System.out.println("pointer:"+raf.getFilePointer());
        System.out.println("搞定，完成");
        raf.seek(0);
        System.out.println(raf.read());
        System.out.println("pointer:"+raf.getFilePointer());

        /*
        RAF提供了方便读取基本类型的相关方法
        int
         */
        int d=raf.readInt();
        System.out.println(d);
        System.out.println("pointer:"+raf.getFilePointer());

        raf.seek(8);
        double dou=raf.readDouble();
        System.out.println(dou);
        long l=raf.readLong();
        System.out.println(l);
        System.out.println(raf.read());


        raf.seek(8);
        raf.writeDouble(234.234);

        raf.seek(8);
      dou =raf.readDouble();
        System.out.println(dou);
        raf.close();
    }

}
